package com.example.demo1.controller;

import com.example.demo1.common.Result;
import com.example.demo1.entity.PostEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;

@CrossOrigin
@ResponseBody
@RestController
@RequestMapping("api/test")
//说明该类的作用
//@Api(tags = "一些测试用例")
public class JieKouController {
    @Autowired
    private RestTemplate restTemplate;
    @PostMapping("/get")
    public Result getResponse(@RequestBody String json) throws JSONException {
        JSONObject obj = new JSONObject(json);
        String url = obj.getString("url");
        //1. getForObject()
        //先获取返回的字符串，若想获取属性，可以使用gson转化为实体后get方法获取
        String result = restTemplate.getForObject(url, String.class);
        System.out.println(result);//{"code":"0","data":{"address":"北京市海淀区","id":1,"password":"123456","role":0,"sex":0,"telephone":"10086","username":"小明"},"msg":"操作成功"}

        //2. getForEntity()
        //获取实体ResponseEntity，可以用get函数获取相关信息
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        System.out.println("responseEntity.getStatusCode() = " + responseEntity.getStatusCode());

        System.out.println("responseEntity.getStatusCodeValue() = " + responseEntity.getStatusCodeValue()); //responseEntity.getStatusCodeValue() = 200

        System.out.println("responseEntity.getBody() = " + responseEntity.getBody());   //responseEntity.getBody() = {"code":"0","data":{"address":"北京市海淀区","id":1,"password":"123456","role":0,"sex":0,"telephone":"10086","username":"小明"},"msg":"操作成功"}

        System.out.println("responseEntity.getHeaders() = " + responseEntity.getHeaders());//responseEntity.getHeaders() = [Content-Type:"application/json", Content-Length:"158", Server:"Werkzeug/0.14.1 Python/3.7.0", Date:"Sat, 16 Oct 2021 06:01:26 GMT"]

        System.out.println("responseEntity.getClass() = " + responseEntity.getClass());//responseEntity.getClass() = class org.springframework.http.ResponseEntity
        return new Result(200,"123",responseEntity);

    }

    @PostMapping("/post")
    public Result postResponse(@RequestBody PostEntity json) throws JSONException {
        //String url = "http://127.0.0.1:5000/register";
        //String url = "http://127.0.0.1:5000/register";
        System.out.println("----------------------------------------------------------");
        String url = json.getUrl();
        String username=json.getUsername();
        String password=json.getPassword();
        //LinkedMultiValueMap一个键对应多个值，对应format-data的传入类型
        LinkedMultiValueMap<String, String> request = new LinkedMultiValueMap<>();
        //入参
        request.set("username",username);
        request.set("password", password);
//        request.set("sex", "0");
//        request.set("telephone", "13172724946");
        //请求
        String result = restTemplate.postForObject(url,request,String.class);
        System.out.println(result);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
        System.out.println("responseEntity.getBody() = " + responseEntity.getBody());

        return Result.success(responseEntity);
    }

}

